let tel = location.search.split('=')[1];

let dialogwrite = document.querySelector('.dialogwrite');

dialogwrite.innerHTML = `<span>账号</span><span>${tel}</span>`;

jQuery.validator.addMethod('testEmail', function (value) {

    let emailReg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/i;

    return emailReg.test(value);

});

jQuery.validator.addMethod('testPassword', function (value) {

    // 至少8个字符，至少一个字母和至少一个数字

    let passwordReg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/i;

    return passwordReg.test(value);

});

$('#complete').validate({

    rules: {

        email: {
            required: true,
            testEmail: true
        },
        nickname: {
            required: true,
        },
        password1: {
            required: true,
            testPassword: true
        },
        password2: {
            required: true,
            equalTo: "#psd"
        },
        select: {
            required: true,
        }
    },

    messages: {
        email: {
            required: '邮箱是必填的',
            testEmail: '请输入正确的邮箱'
        },
        nickname: {
            required: '昵称是必填的',
        },
        password1: {
            required: '请填写密码',
            testPassword: '至少8个字符，至少一个字母和至少一个数字'
        },
        password2: {
            required: '请再次确认密码',
            equalTo: '两次密码不一样'
        },
        select: {
            required: '请阅读条款并同意',
        }
    },

    // 已经做了取消自动刷新功能
    submitHandler: function () {

        let btn = document.querySelector('#btn');

        let email = document.querySelector('#email').value;
        let nickname = document.querySelector('#nickname').value;
        let password = document.querySelector('#psd').value;

        btn.onclick = function () {

            let xhr = new XMLHttpRequest();

            xhr.open('get', `http://localhost:8080/dialog?tel=${tel}&email=${email}&nickname=${nickname}&password=${password}`);

            xhr.send();

            xhr.onload = function () {

                console.log(xhr.responseText)

                let res = JSON.parse(xhr.responseText);

                if (res.code == true) {
                    setCookie('user',nickname,5);
                    alert(res.msg);
                    location.href = `../html/index.html`;
                    return;
                };
                alert(res.msg);
            }
        }
    }
});